const categoryService = require('../service/categoryService');
const router = require('koa-router')();

router.prefix('/api/category');

/**
 * @api {post} /api/category/addCategory 添加类目
 * @apiName addCategory
 * @apiGroup category
 *
 * @apiParam {String} title 类目名称.
 * @apiParam {String} number 序号.
 * @apiParam {String} content 内容.
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/addCategory', async (ctx) => {
    try {
        let data = ctx.request.body;
        let title = data.title,
            number = data.number,
            content = data.content;
        if (!title || !number || !content) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await categoryService.handleAddCategory(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/category/editCategory 修改类目
 * @apiName editCategory
 * @apiGroup category
 *
 * @apiParam {String} id 类目id.
 * @apiParam {String} title 类目名称.
 * @apiParam {String} number 序号.
 * @apiParam {String} content 内容.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editCategory', async (ctx) => {
    try {
        let data = ctx.request.body;
        let title = data.title,
            number = data.number,
            content = data.content,
            id = data.id;
        if (!title || !number || !content || !id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await categoryService.handleEditCategory(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/category/deleteCategory 删除类目
 * @apiName deleteCategory
 * @apiGroup category
 *
 * @apiParam {String} id 类目id.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/deleteCategory', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await categoryService.handleDeleteCategory(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/category/getCategory 获取类目列表
 * @apiName getCategory
 * @apiGroup category
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {json} data 获取成功时返回的数据
 * @apiSuccess {string} picture 主图url
 */

router.post('/getCategory', async (ctx) => {
    try {
        let result, result1;
        result = await categoryService.handleGetCategory();
        result1 = await categoryService.handleGetImgUrl();
        if (result1.code === 1) {
            result.picture_url = result1.data[0].picture_url;
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

module.exports = router;